package com.example.controller;

import com.example.common.Result;
import com.example.entity.Address;
import com.example.entity.BooksHistory;
import com.example.service.BooksHistoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 历史记录书籍前端接口
 */
@RestController
@Api(value = "书籍的阅读记录", tags = {"书籍阅读记录的增删改查"})
@RequestMapping("/booksHistory")
public class BooksHistoryController {

    @Resource
    private BooksHistoryService booksHistoryService;

    /**
     * 新增
     */
    @ApiOperation(value = "增加记录", notes = "返回是否成功")
    @PostMapping("/add")
    public Result add(@RequestBody BooksHistory booksHistory) {
        List<BooksHistory> list = booksHistoryService.selectAll(booksHistory);

        if (list != null && !list.isEmpty()) {
            // 如果List有结果，则退出
            return Result.success("List already contains data. Exiting without adding.");
        } else {
            // 否则执行添加操作
            booksHistoryService.add(booksHistory);
            return Result.success("Data added successfully.");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除记录", notes = "返回是否成功")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要删除的记录id,如'1'"
            )
    })
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        booksHistoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除的记录", notes = "返回是否成功")
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        booksHistoryService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新阅读记录", notes = "返回是否成功")
    @PutMapping("/update")
    public Result updateById(@RequestBody BooksHistory booksHistory) {
        booksHistoryService.updateById(booksHistory);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "通过id查询阅读记录", notes = "返回Models中的阅读记录类型")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要查询的阅读记录数据库id,如'1'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = BooksHistory.class,reference = "BooksHistory")
    })
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        BooksHistory booksHistory = booksHistoryService.selectById(id);
        return Result.success(booksHistory);
    }

    /**
     * 查询所有
     */
    @ApiOperation(value = "查询所有符合特征的阅读记录", notes = "返回Models中的阅读记录类型的数组")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = BooksHistory.class,reference = "BooksHistory")
    })
    @GetMapping("/selectAll")
    public Result selectAll(BooksHistory booksHistory ) {
        List<BooksHistory> booksHistoryList = booksHistoryService.selectAll(booksHistory);
        return Result.success(booksHistoryList);
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询所有符合特征的阅读记录", notes = "返回Models中的阅读记录的分页数组")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "pageNum",
                    value = "要查询的页码,默认为'1'"
            ),
            @ApiImplicitParam(
                    name = "pageSize",
                    value = "每页尺寸,默认为'10'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = BooksHistory.class,reference = "BooksHistory")
    })
    @GetMapping("/selectPage")
    public Result selectPage(BooksHistory booksHistory,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<BooksHistory> pageInfo = booksHistoryService.selectPage(booksHistory, pageNum, pageSize);
        return Result.success(pageInfo);
    }

}
